from django.shortcuts import render
from django.views import View
from django.http import JsonResponse


from user.models import User

# Create your views here.
class UserNameCountView(View):
    """
    判断用户是否重复
    """
    def get(self, request, username):
        """
        :param request: 请求报文对象
        :param username: 前端传递的用户名信息
        :return: 返回查询到的用户数量 count
        """
        try:
            count = User.objects.filter(username=username).count()
        except Exception as e:
            return JsonResponse({
                # 自定义状态码 http协议中 状态码是1xx,2xx,3xx,4xx,5xx 公司自定义的状态码可以是9xx
                "code": 400,
                "errmsg": e
            }, status=400)
        else:
            return JsonResponse({"count": count})


class MobilesCountView(View):
    """
    判断手机号重复
    """
    def get(self, request, mobile):
        """
        :param request: 请求报文对象
        :param username: 前端传递的手机号信息
        :return: 返回查询到的用户数量 count
        """
        try:
            count = User.objects.filter(mobile=mobile).count()
        except Exception as e:
            return JsonResponse({
                # 自定义状态码 http协议中 状态码是1xx,2xx,3xx,4xx,5xx 公司自定义的状态码可以是9xx
                "code": 400,
                "errmsg": e
            }, status=400)
        else:
            return JsonResponse({"count": count})
